package cn.pku.eecs.ldn.view.listview;

import cn.pku.eecs.ldn.R;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class HeaderView extends LinearLayout {
	private static final String TAG = "HeaderView";
	private static final boolean DEBUG = true;

	private View headerView;
	private ImageView imageViewArrow;
	private ProgressBar progressBarLoading;
	private TextView textViewHint;
	private TextView textViewLastUpdateTime;

	private Animation animationRotateUp;
	private Animation animationRotateDown;

	private final int ROTATE_ANIM_DURATION = 180;

	public final static int STATE_NORMAL = 0;
	public final static int STATE_READY = 1;
	public final static int STATE_REFRESHING = 2;
	public final static int STATE_EMPTY = 3;
	private int state = STATE_NORMAL;

	private int contentViewHeight = 0;

	public HeaderView(Context context) {
		super(context);
		initView(context);
	}

	public HeaderView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	private void initView(Context context) {
		if (DEBUG)
			Log.d(TAG, "initView()");
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, 0);
		headerView = LayoutInflater.from(context).inflate(
				R.layout.pull_up_and_down_listview_header, null);
		addView(headerView, lp);

		imageViewArrow = (ImageView) findViewById(R.id.imageViewArrow);
		textViewHint = (TextView) findViewById(R.id.textViewHint);
		textViewLastUpdateTime = (TextView) findViewById(R.id.textViewLastUpdateTime);
		progressBarLoading = (ProgressBar) findViewById(R.id.progressBarLoading);

		animationRotateUp = new RotateAnimation(0.0f, -180.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		animationRotateUp.setDuration(ROTATE_ANIM_DURATION);
		animationRotateUp.setFillAfter(true);
		animationRotateDown = new RotateAnimation(-180.0f, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		animationRotateDown.setDuration(ROTATE_ANIM_DURATION);
		animationRotateDown.setFillAfter(true);
	}

	public int getContentViewHeight() {
		if (contentViewHeight == 0) {
			headerView.measure(
					MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
					MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			contentViewHeight = headerView.getMeasuredHeight();
		}
		if (DEBUG)
			Log.i(TAG, "getContentViewHeight(): " + contentViewHeight);
		return contentViewHeight;
	}

	/**
	 * set the display state of this view
	 * 
	 * @param state
	 */
	public void setState(int newState) {
		if (state == newState)
			return;
		if (DEBUG)
			Log.d(TAG, "setState(): from " + state + " to " + newState);
		if (newState == STATE_EMPTY) {
			imageViewArrow.clearAnimation();
			this.setVisibility(View.INVISIBLE);
			state = newState;
			return;
		} else {
			this.setVisibility(View.VISIBLE);
		}
		progressBarLoading.setVisibility(View.VISIBLE);
		imageViewArrow.setVisibility(View.VISIBLE);
		switch (newState) {
		case STATE_NORMAL:
			progressBarLoading.setVisibility(View.INVISIBLE);
			if (state == STATE_READY) {
				imageViewArrow.startAnimation(animationRotateDown);
			}
			textViewHint.setText(R.string.listview_header_hint_normal);
			break;
		case STATE_READY:
			progressBarLoading.setVisibility(View.INVISIBLE);
			if (state == STATE_NORMAL) {
				imageViewArrow.startAnimation(animationRotateUp);
			}
			textViewHint.setText(R.string.listview_header_hint_ready);
			break;
		case STATE_REFRESHING:
			imageViewArrow.clearAnimation();
			imageViewArrow.setVisibility(View.INVISIBLE);
			textViewHint.setText(R.string.listview_header_hint_loading);
			break;
		default:
			if (DEBUG)
				Log.e(TAG, "setState(): unknown state");
			break;
		}
		state = newState;
	}

	public void setLastUpdatedTime(String timeString) {
		if (textViewLastUpdateTime == null) {
			if (DEBUG)
				Log.e(TAG,
						"setLastUpdatedTime(): textViewLastUpdateTime is null");
			return;
		}
		textViewLastUpdateTime.setText(timeString);
	}

	public void setHeight(int height) {
		if (height < 0) {
			height = 0;
		}
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) headerView
				.getLayoutParams();
		lp.height = height;
		headerView.setLayoutParams(lp);
		if (DEBUG)
			Log.d(TAG, "setHeight(): " + height);
	}
}
